layui.define(['layer'],function (exports) {
    var $ = layui.jquery,layer = layui.layer
        ,setter = layui.setter;
    var initOptions = {
        title:'',   //zTree页面 title
        url:'',     //nodes 请求地址
        echoId:'',  //回显元素id
        excludeCode:'', //排除节点ID
        isShowCode:'false',   //是否显示节点代码 默认不显示
        canSelectRoot:true,     //根节点 是否允许选择，默认可选
        canSelectParent:true    //父节点 是否允许选择，默认可选
    },nodes = [],zTreeObj,
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    setting = {
        check: {
            enable: true//checkbox
        },
        view: {
            nameIsHTML: true, //允许name支持html
            selectedMulti: false
        },
        edit: {
            enable: false,
            editNameSelectAll: false
        },
        // 回调定义
        callback:{
            // 鼠标点击事件的回调函数
            onClick:zTreeOnClick
        },
        // zTree data格式定义
        data:{
            keep:{
                leaf:true,
                parent:true
            },
            key:{
                isParent:"treeLeaf"
            },
            simpleData:{
                enable:true,
                idKey:"id",
                pIdKey:"pId",
                rootPid:"0"
            }
        }
    };
    var zTreeTool = {

        zTreeOpen:function (options) {
            let zTreeID = 'treePanel';
            initOptions = intiOption(options);
            $("#zTreeTitle").empty();
            $("#zTreeTitle").append(initOptions.title);
            getNodes();
            loadTree();
            $(".modal-bg").css("display" , "block");
            $(".modal-show").css("display" , "block");
        },
        zTreeOnClick:function (event, treeId, treeNode) {
            $("#" + initOptions.echoId + "Code").val(treeNode.id);
            $("#" + initOptions.echoId + "Name").val(treeNode.name);
            reloadTreePanelValue(treeNode.id);
            closePanel();
        },
        closePanel:function () {
            $(".modal-bg").css("display" , "none");
            $(".modal-show").css("display" , "none");
        },
        getTreeOptions:function () {
            if (zTreeObj != '' && zTreeObj != undefined && zTreeObj != null && zTreeObj != 'null'){
                return initOptions;
            }
        },
        reloadTreePanelValue:function (parentCode) {
            reloadTreePanelValue(parentCode);
        }
    };

    function intiOption(userOptions) {
        $.extend(true, initOptions, userOptions);
        return initOptions;
    };

    function getNodes() {
        let excludeCodeVal = $("#" + initOptions.excludeCode).val();
        let excludeCode = excludeCodeVal != '' && excludeCodeVal != undefined?"excludeCode=" + excludeCodeVal:'';
        let isShowCode = initOptions.isShowCode != '' || initOptions.isShowCode != undefined?"isShowCode=" + initOptions.isShowCode:'';
        // var url = initOptions.url + "?excludeCode=" + excludeCode + "&isShowCode=" + initOptions.isShowCode;
        let url = initOptions.url;
        if (isNotEmpty(excludeCode) && isNotEmpty(isShowCode)){
            url = url + "?" + excludeCode + "&" + isShowCode;
        }else if (isNotEmpty(excludeCode)){
            url = url + "?" + excludeCode;
        }else if (isNotEmpty(isShowCode)){
            url = url + "?" + isShowCode;
        }
        $.ajax({
            type:"get",
            url:url,
            dataType:'json',
            async:false,
            success:function (result) {
                if (isNotEmpty(result)){
                    nodes = result;
                }
            },
            error:function () {

            }
        });
    };

    function loadTree() {
        //搜索框
        $("#searchBtn").on("click" , function () {
            fuzzSearch('treePanel','#searchInput',null,true); //初始化模糊搜索方法
        });
        //zTree加载
        zTreeObj = $.fn.zTree.init($("#treePanel"), setting, nodes);
        zTreeObj.expandAll(false);
        fuzzSearch('treePanel','#searchInput',null,true); //初始化模糊搜索方法
    };

    /**
     * 生成 新代码 & 排序序号
     * @param parentCode
     */
    function reloadTreePanelValue(parentCode){
        var excludeCode = $("#" + initOptions.excludeCode).val();
        var data = {parentCode:parentCode,id:excludeCode};
        var url = initOptions.url.replace("treeData" , "createNextNode");
        $.ajax({
            type:"POST",
            url:url,
            dataType:"JSON",
            cache: false,
            headers : {
                'Content-Type' : 'application/json;charset=utf-8'
            },
            data:JSON.stringify(data),
            success:function (result) {
                if (isNotEmpty(result)){
                    var _treeSort = result.treeSort;
                    var _treeCode = result.treeCode;
                    var sortElement = $("#treeSort");
                    if (isNotEmpty(sortElement)){
                        $("#treeSort").val(_treeSort);
                    }
                    var treeCodeElement = $("#treeCode");
                    if (isNotEmpty(treeCodeElement)){
                        $("#treeCode").val(_treeCode);
                    }
                }
            },
            error:function () {

            }

        });
    };

    function closePanel() {
        $(".modal-bg").css("display" , "none");
        $(".modal-show").css("display" , "none");
    };

    exports('zTreeTool', zTreeTool);
});